var PassCardId = 0;

exports.go = function(player, req, resp, onHandled) {
    do {
        var costType = req.args.type ? req.args.type : 0;
        if( costType && !('cash' == costType || 'gold' == costType) ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var user = player.user;
        var business = user.business;
        if( !gConfBusiness[business.id+1] || business.back >= gConfLevel[user.status.level].BusinessGobackLimit ) {
            resp.code = 1; resp.desc = 'invalid oop'; break;
        }

        var passed = false;
        if( business.cards.indexOf(PassCardId) >= 0 ) {
            // 存在通关卡牌
            passed = true;
        }

        var newId = business.id;
        if( passed ) {
            if( costType ) {
                resp.code = 1; resp.desc = 'can not use resource'; break;
            }
            // 通关后,走到下一城市
            newId = business.id + 1;
        }

        // 已经走过,但是没有通关,并且不用Cash or Gold
        if( !passed && (business.cards.length > 0) && !costType) {
            resp.code = 1; resp.desc = 'not pass and no resource to go'; break;
        }

        var businessConf = gConfBusiness[newId];
        var costNum=0;
        if( costType ) {
            costNum = +businessConf[costType.capWord()];
            if (!player.costResource(costType, costNum, business.id)) {
                resp.code = 1; resp.desc = 'not enough resource'; break;
            }
        }
        
        // 先找出所有卡本次选出对应的宝石
        var cardGems = [];
        for( var i=1; i<=8; i++ ) {
            cardGems.push(common.randArray(businessConf['Gem'+i]));
        }

        var selectedCardNum = 5;
        if( gConfBusiness[newId+1] && 
            common.randRange(1, 100) <= businessConf.CardPass ) {
            // 不是最后一关，并且通关,4个奖励卡牌
            selectedCardNum = 4;
        }

        var allCards = {};
        for( var i=1; i<=8; i++ ) {
            allCards[i] = businessConf['Card'+i];
        }

        var selectedCards = [];
        for( var i=1; i<=selectedCardNum; i++ ) {
            // 随机找到卡对应的宝石
            var card = common.wRand(allCards);
            selectedCards.push( cardGems[card-1] );
            delete allCards[card];
        }
        
        if( selectedCardNum == 4 ) {
            // 把通关的卡牌Id随机插入翻牌结果中
            var index = common.randRange(0,3);
            selectedCards.splice(index, 0, PassCardId); 
        }
        
        business.id = newId;
        business.opened = [];
        business.cards = selectedCards;
        player.markDirty('business');
        
        // 最后一关的奖励
        if( !gConfBusiness[newId+1] ) {
            var gem = common.randArray(businessConf.GemFinal) 
            player.addInventory('gem', gem, 1);
            resp.data.gem = gem;
        }

        player.doDailyTask(gConfDailyTask.Business);

        resp.data.business = user.business;
        resp.data.cost = -costNum;
        resp.data.all_cards = cardGems;
        resp.daily_task = gConfDailyTask.Business;

    }while(false);

    onHandled();
}

exports.open = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.pos) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var pos = +req.args.pos;

        var user = player.user;
        var cards = user.business.cards;
        var opened = user.business.opened;

        if( cards.length < 5 ) {
            resp.code = 1; resp.desc = 'not go here'; break;
        }

        if( opened.length >= cards.length ) {
            resp.code = 1; resp.desc = 'not more card to open'; break;
        }

        var businessConf = gConfBusiness[user.business.id];
        var goldCost = +businessConf.GoldOpen;
        if( user.status.gold < goldCost ) {
            resp.code = 1; resp.desc = 'not enough gold'; break;
        }
        
        var gem = cards[opened.length];
        if( gem != PassCardId ) {
            // 给对应宝石奖励
            player.addInventory('gem', gem, 1);
        }

        user.status.gold -= goldCost; 
        player.markDirty('status.gold');

        opened.push(pos);
        player.markDirty('business.opened');

        resp.data.gem = gem;
        resp.data.gold = -goldCost;

    }while(false);

    onHandled();
}

exports.back = function(player, req, resp, onHandled) {
    do {
        var business = player.user.business;
        if( business.back >= gConfLevel[player.user.status.level].BusinessGobackLimit ) {
            resp.code = 1; resp.desc = 'no time'; break;
        }

        business.id = 1;
        business.cards = [];
        business.opened = [];
        business.back += 1;

        player.markDirty('business');
        
    }while(false);

    onHandled();
}

exports.auto_open = function (player, req, resp, onHandled){
    do {
        var user = player.user;
        var cards = user.business.cards;
        var opened = user.business.opened;
        if ( opened.length != 0 || cards.length != 5 ){
            resp.code = 1; resp.desc = 'invalid oop'; break;
        }
        
        var businessConf = gConfBusiness[user.business.id];
        var goldCost = businessConf.GoldOpen * 5;
        if (user.status.gold < goldCost ){
            resp.code = 1; resp.desc = 'not enough gold'; break;
        }

        var awards = [];
        for( var k=0, len = cards.length; k<len; k++ ) {
            if ( cards[k] != PassCardId ) {
                player.addInventory('gem', cards[k], 1);
                awards.push(['gem', cards[k], 1]);
            }
        }   
        
        var randNums= [1,2,3,4,5,6,7,8,9];
        var selectNums = [];
        for( var i=1; i<=5; i++ ) {
            var num =  common.randArray(randNums);  
            randNums.remove(num);
            selectNums.push(num);
        }

        user.status.gold -= goldCost;
        player.markDirty('status.gold');

        user.business.opened = selectNums;
        player.markDirty('business.opened');

        resp.data.gold = -goldCost;
        resp.data.awards = awards;
        resp.data.opened = selectNums;

    }while (false);

    onHandled();
}
